var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        activeKey:0,
        ProductType:'',//1-优惠券选择商品，2-活动选择商品
        sortList:[],
        productList:[],
        show:false,
        selectid:0,
        openid:0,
        selectitemid:0,
        show_pro:false,
        IsChoose_pro:false,
        selected_proid:0,
        
        choose_prolist:[],
        choosenum:0,
        CategoryIDString:[],
        Type:1,//1-打折，2-降价
        zhekou:false,//是否是营销活动的折扣类别

    },
    methods: {
        get_data:function(){
            var that=this;
            that.get_menus();
            that.get_productlist();

            // setTimeout(() => {
            //     that.find_product();
            // }, 1000);
            
        },
        get_productlist:function(){
            var that=this;
            that.productList=[];
            var option={};
            option.pageindex=that.pageindex;
            option.pagesize=that.pagesize;
            option.Keywords=that.Keywords;
            option.CategoryIDString=that.CategoryIDString;
            option.ProductType=that.ProductType;
            option.action='selectproductlist';
            console.log(JSON.stringify(option));
            ns.post(option, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if(succeed){
                    that.productList=data.productList;
                    that.productList=that.productList.map(res=>{
                        res.isSelect=false;
                        return res;
                    });
                    if(that.choose_prolist!=undefined&&that.choose_prolist.length>0){
                        that.find_product(that.choose_prolist);
                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    }); 
                }
                
            });


        },
        //获取商品分类
        get_menus:function () {
            var that=this;
            var option = {};
            option.action = "getsearcherproductlistparams";
            ns.post(option, function(succeed, data, err) {
                if (succeed) {
                    const List=data.CategoryList;
                    that.sortList = that.buildTree(List, 0, "ID", "ParentID");
                    that.sortList.splice(0,0,{"ID":0, "ParentID":0, "Name":"全部商品", "Level":1, "Sort":1, "IsSelected":true});
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //处理树
        buildTree:function(arr, root, key, pKey) {
            var that=this;
            const temp = [];
            for (const item of arr) {
                if (Number(item[pKey]) === Number(root)) {
                    const children = that.buildTree(arr, item[key], key, pKey);
                    if (children.length > 0) item.children = children
                    temp.push(item);
                }
            }
            return temp;
        },
        // 选择左边菜单
        select_pro:function(item){
            var that=this;
            if(item.children==undefined||item.children.length<=0){
                that.selectid=item.ID;
                that.openid=0;
                that.CategoryIDString=[];
                if(item.ID==0){
                    that.CategoryIDString=[];
                }else{
                    that.CategoryIDString.push(that.selectid);
                }
            }else{
                that.openid=item.ID;
                that.selectid=item.ID;
                that.selectitemid=item.children[0].ID;
                that.CategoryIDString=[];
                that.CategoryIDString.push(that.selectitemid);
            }
            that.get_productlist();
            
        },
        // 选择左边子菜单
        select_prolist:function(item){
            var that=this;
            that.selectitemid=0;
            that.selectitemid=item.ID;
            that.CategoryIDString=[];
            that.CategoryIDString.push(that.selectitemid);
            that.get_productlist();
            // if(that.choose_prolist!=undefined&&that.choose_prolist.length>0){
            //     that.find_product(that.choose_prolist);
            // }
        },
        //活动内容
        // do_openhuodong:function(){
        //     var that=this;
        //     ns.openWin('activity_detail', '活动内容');
        // },
        //添加新活动
        do_addproduct:function(){
            var that=this;
            if(that.ProductType=='member'){
                var v_prolist=[];
                for(var i=0;i<that.choose_prolist.length;i++){
                    v_prolist.push({ID:that.choose_prolist[i].ID,MemberPrice:that.choose_prolist[i].MemberPrice,});
                }
                var option = {};
                option.action = "savemembercardproduct";
                option.Products=v_prolist;
                console.log(JSON.stringify(option));
                ns.post(option, function(succeed, data, err) {
                    console.log(JSON.stringify(data));
                    console.log(JSON.stringify(err));
                    if (succeed) {
                        if(data.status){
                            api.toast({
                                msg: data.msg,
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.sendEvent({
                                name:'load_productvip'
                            })
                            setTimeout(() => {
                                api.closeWin()
                            }, 400);
                        }
                        
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });

                
            }else{
                api.sendEvent({
                    name: 'do_add_product',
                    extra: {
                        list: that.choose_prolist,
                    }
                });
                api.closeWin();
            }
           
        },
        // 选择商品
        do_editproduct:function(item){
            var that=this;
            if(that.ProductType=='activity'||that.ProductType=='member'){
                that.zhekou=false;
                that.show_pro=true;
                that.cen_winobj=item;
                if(that.Type==1){
                    that.zhekou=true;
                }
                console.log(JSON.stringify(item));
            }else{
                item.isSelect=true;
                // console.log(JSON.stringify(that.choose_prolist));
                that.choose_prolist.push(item);
                that.choosenum=that.choose_prolist.length;
            }
            

        },
        // 弹窗保存修改商品
        save_editpro:function(id){
            var that=this;
            if (that.ProductType=='activity'&&!that.cen_winobj.ActivityPrice&&!that.zhekou) {
                api.toast({
                    msg: '活动单价不能为空',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.ProductType=='member'&&!that.cen_winobj.MemberPrice) {
                api.toast({
                    msg: '会员价不能为空',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.ProductType=='member'&&that.cen_winobj.MemberPrice>that.cen_winobj.SalePrice) {
                api.toast({
                    msg: '会员价不能大于销售价',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.cen_winobj.LimitCount) {
                that.cen_winobj.LimitCount=0;
            }
            that.cen_winobj.isSelect=true;
            that.choose_prolist.push(that.cen_winobj);
            that.choosenum=that.choose_prolist.length;
            that.show_pro=false;
        },
        //打开选择的商品
        do_choose_productlist:function(){
            var that=this;
            if(!that.choosenum){
                api.toast({
                    msg: '没有选择的商品',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            that.show=true;
        },
        closebox:function(){
            var that=this;
            that.show=false;
            that.show_pro=false;
        },
        do_remove:function(id){
            var that=this;
            that.choose_prolist = that.choose_prolist.filter((item) => {
                return item.ID != id
                });
            that.choosenum=that.choose_prolist.length;
            that.get_productlist();
            if(that.choose_prolist.length<=0){
                that.show=false;
            }
        },
        // do_edit:function(item){
        //     var that=this;
        //     that.show_pro=true;
        //     that.cen_winobj.Name=item.Name;
        //     that.cen_winobj.SalePrice=item.SalePrice;
        //     that.cen_winobj.ActivityPrice=item.ActivityPrice;
        //     that.cen_winobj.LimitCount=item.LimitCount;
        // },

        //查找相同的元素
        find_product:function(list){
            var that=this;
            for(let item of that.productList){
               const flag=list.some(function matcher(p){
                    return p.ID==item.ID;
                });
                item.isSelect=flag;
            }
            // that.productList.map(res=>{
            //     const {ID} = res
            //     const has = list.find(r=> Number(r.ID) === Number(ID))
            //     res.isSelect = has !== undefined
            //     return res
            // })
        },
    }
       


      
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.ProductType = api.pageParam.ProductType;//1-优惠券选择商品，2-活动选择商品,3-会员商品选择商品
    app.Type= api.pageParam.Type;//1-打折，2-降价
    console.log(JSON.stringify(app.ProductType));
    app.choose_prolist = api.pageParam.list||[];
    if(app.choose_prolist!=undefined&&app.choose_prolist.length>0){
        app.choosenum=app.choose_prolist.length;
    }else{
        app.choosenum=0;
    }
    
    app.get_data();

};
